package com.boco.pciv.controller.sys;

import cn.hutool.core.util.StrUtil;
import com.boco.pciv.controller.PcivController;
import com.boco.pciv.core.entity.*;
import com.boco.pciv.core.exception.PcivException;
import com.boco.pciv.entity.sys.DataDict;
import com.boco.pciv.entity.sys.DictDetailInfo;
import com.boco.pciv.service.sys.IDataDictService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value="/sys/dict")
public class DataDictController extends PcivController {
	@Resource
	private IDataDictService idds;
	private  static Logger logger = LoggerFactory.getLogger(DataDictController.class);

	@GetMapping("/list")
	public PageResultBase getDataDictList(String dictType, LayUIPager pageParam){
		SearchCriteria param = new SearchCriteria();
		param.setPager(pageParam);
		param.putCriteria("MutiFlag", "0");  //查询单条数据字典
		param.putCriteria("DictTypeCode", dictType);
		PageResultBase result=idds.getPage(param);
		addOperationLog("查询","数据字典信息查询");
		return result;
	}

	@GetMapping("/listMuti")
	public BizResponse getDataDictListMuti(String DictTypeCode){
		List<DataDict> result=idds.getListByType(DictTypeCode,"");
		addOperationLog("查询","多层数据字典信息查询");
		return BizResponseUtil.list(result);
	}

	@GetMapping("/listByParent")
	public BizResponse getDataDictListByParent(String dictTypeCode, String dictKey){
		if (StrUtil.isBlank(dictKey)) {
			dictKey = "00";
		}
		List<DataDict> result=idds.getListByType(dictTypeCode,dictKey);
		return  BizResponseUtil.list(result);
	}

	@GetMapping("/dictTree")
	public BizResponse getDataDictTree(String dictTypeCode, String parentKey){
		List<BizTree> trees = new ArrayList<BizTree>();
		BizTree tree = idds.getDataDictTreeByParent(dictTypeCode,parentKey);
		trees.add(tree);
		return BizResponseUtil.single(tree);
	}

	@PostMapping("/add")
	public BizResponse addDataDict(DictDetailInfo data){
		try{
			data.setCreateUser(getCurUserId());
			idds.add(data);
			addOperationLog("新增",String.format("新增数据字典:%s",data.getDictKey()));
			return BizResponseUtil.success(MSG_ExeOK);
		} catch (PcivException ex){
			logger.warn(MSG_ExeERROR, ex);
			return BizResponseUtil.error(ex);
		} catch(Exception e){
			logger.warn(MSG_ExeERROR, e);
			return BizResponseUtil.fail(MSG_ExeERROR+e.getMessage());
		}
	}

	@GetMapping("/info")
	public BizResponse getDataDict(Integer id){
		DictDetailInfo result=idds.getOne(id);
		return BizResponseUtil.single(result);
	}

	@PostMapping("/edit")
	public BizResponse editDataDict(DictDetailInfo data, Integer id){
		try{
			data.setUpdateUser(getCurUserId());
			idds.edit(data);
			addOperationLog("编辑",String.format("编辑数据字典%s信息",data.getDictKey()));
			return BizResponseUtil.success(MSG_ExeOK);
		} catch (PcivException ex){
			logger.warn(MSG_ExeERROR, ex);
			return BizResponseUtil.error(ex);
		} catch(Exception e){
			logger.warn(MSG_ExeERROR, e);
			return BizResponseUtil.fail(MSG_ExeERROR+e.getMessage());
		}
	}

	@PostMapping("/delete")
	public BizResponse deleteDataDict(Integer id){
		try{
			idds.delete(id);
			addOperationLog("删除",String.format("删除数据字典:%s",id));
			return BizResponseUtil.success(MSG_ExeOK);
		} catch (PcivException ex){
			logger.warn(MSG_ExeERROR, ex);
			return BizResponseUtil.error(ex);
		} catch(Exception e){
			logger.warn(MSG_ExeERROR, e);
			return BizResponseUtil.fail(MSG_ExeERROR+e.getMessage());
		}
	}

	@GetMapping("/checkDictValidate")
	public String checkDictValidate(String dictType, String dictKey){
		DictDetailInfo info = idds.getOneByKey(dictType,dictKey);
		if(info!=null){
			return "false";
		}
		else {
			return "true";
		}
	}

}
